package suanfa.datastructure.stack;

//9.如何实例化一个栈，你能写一个程序，一边压栈一边出栈吗？
public class Stack1 {
    private static class Node{
        public int data;
        public Node next;

        public Node(int data){
            this.next=null;
            this.data=data;
        }
    }
    public int size=0;
    public Node top;

    public boolean isEmpty(){//判空方法
        return top==null;
    }
    public void push(int data){//入栈
        Node newnode=new Node(data);//临时结点
        if(isEmpty()){//判空
            top=newnode;
        }
        else{
            newnode.next=top;
            top=newnode;

            size++;
        }

    }

    public int pop(){//出栈
        if(isEmpty()){
            throw new IllegalStateException("栈已空");
        }else{
            int t=top.data;//存栈顶元素
            System.out.println(t+" 出栈");
            top=top.next;//移动top指针
            return t;
        }
    }
    public void pushpop(int s){
        while(s>0){
            int a=(int)(Math.random()*10);
            System.out.println(a+" 入栈");
            push(a);
            pop();
            s--;
        }
    }

    public static void main(String[] args) {
        Stack1 s=new Stack1();
        s.pushpop(5);
    }

}
